Ext.namespace('Farm');

Farm.SelectDiscountCategoriesControl = function(config){
	
	this.pCategoriesStore = new Ext.data.JsonStore({
		autoLoad: true,
		fields: [{name: 'uid'}, {name: 'title'}],
		data: config.selectedCategories,
		id: 'uid'
	});
	
	console.debug(this.pCategoriesStore);
	
	Farm.SelectDiscountCategoriesControl.superclass.constructor.call(this, {
		applyTo: Ext.get("data[user_farm_commerce_discounts][" + config.uid + "][categories_panel]"),
		layout: "column",
		width: 528,
		height: 300,
		header: false,
		border: false,
		items: [{
			width: 264,
			title: "Выбрано:",
			xtype: "grid",
			store: this.pCategoriesStore,
			columns: [{id: "uid", header: "ID", dataIndex: "uid", hidden: true},
					  {id: "title", header: "Название", dataIndex: "title"}],
			viewConfig: {
				forceFit: true
			},
			sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
			cls: 'catpanel',
			autoExpandColumn: "title",
			enableHdMenu: false,
			hideHeaders: true,
			tbar: [{
				id: "discountCategoriesRemoveButton",
				iconCls: "delete-discount-category",
				text: "Удалить",
				handler: this.removeCategory,
				scope: this
			}],
			border: true,
			bodyBorder: true,
			height: 300,
			autoScroll: true
		},{
			width: 264,
			title: "Категории:",
			xtype: "treepanel",
			root: new Ext.tree.AsyncTreeNode({
				loader: new Ext.tree.TreeLoader({
					dataUrl: "/typo3/ajax.php",
					baseParams: {ajaxID: "FarmDiscounts::getSubcategories"},
					listeners: {
						"beforeload": function(tree, node) {
							tree.baseParams.parentID = node.attributes.uid
						}
					}
				}),
				text: "Категори товаров",
				uid: 1
			}),
			tbar: [{
				iconCls: "refresh-discount-category-tree",
				text: "Обновить",
				handler: this.refreshCategoryTree,
				scope: this
			}, {
				iconCls: "collapse-discount-category-tree",
				text: "Свернуть все",
				handler: this.collapseCategoryTree,
				scope: this
			}],
			border: true,
			bodyBorder: true,
			height: 300,
			autoScroll: true
		}]
	});
}
Ext.extend(Farm.SelectDiscountCategoriesControl, Ext.Panel, {
	removeCategory: function() {
		
	},
	refreshCategoryTree: function() {
		
	},
	collapseCategoryTree: function() {
		
	}
});